<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>LTimer</title>
    <script type="text/javascript" src="lufylegend-1.9.3.min.js"></script>
    <script type="text/javascript" src="lufylegend.ui-0.10.0.min.js"></script>
    <script type="text/javascript" src="../src/LTimer.js"></script>
</head>

<body style="margin:0px 0px 0px 0px;">
    <div id="content"></div>
    <script type="text/javascript"> 
        init(20,"content",600,450,main);
        var timer, delayText, repeatCountText, repeatState, runningState;
        function main(){
            LGlobal.setDebug(true);
            timer=new LTimer(100);
            timer.addEventListener(LTimerEvent.TIMER, onTimer);
            timer.addEventListener(LTimerEvent.TIMER_COMPLETE, onTimerComplete);
            var layer=new LSprite();
            addChild(layer);

            // 设置
            var label=new LTextField();
            label.text="delay: ";
            label.textAlign="rihgt";
            label.width=50;
            label.x=10;
            label.y=10;
            layer.addChild(label);
            delayText=new LTextField();
            delayText.setType(LTextFieldType.INPUT);
            delayText.width=30;
            delayText.text=timer.getDelay();
            delayText.x=label.x+50;
            delayText.y=label.y;
            layer.addChild(delayText);

            var label2=new LTextField();
            label2.text="repeatCount: ";
            label2.textAlign="rihgt";
            label2.width=90;
            label2.x=label.x;
            label2.y=50;
            layer.addChild(label2);
            repeatCountText=new LTextField();
            repeatCountText.setType(LTextFieldType.INPUT);
            repeatCountText.text="0";
            repeatCountText.x=label2.x+label2.width;
            repeatCountText.y=label2.y;
            layer.addChild(repeatCountText);

            var settingButton=new LButtonSample1("setting");
            settingButton.x=label2.x;
            settingButton.y=80;
            settingButton.addEventListener(LMouseEvent.MOUSE_UP, onClickSetting);
            layer.addChild(settingButton);

            // 控制
            var startButton=new LButtonSample1("start");
            startButton.x=label2.x;
            startButton.y=150;
            startButton.addEventListener(LMouseEvent.MOUSE_UP, onClickStart);
            layer.addChild(startButton);

            var stopButton=new LButtonSample1("stop");
            stopButton.x=startButton.x+100;
            stopButton.y=startButton.y;
            stopButton.addEventListener(LMouseEvent.MOUSE_UP, onClickStop);
            layer.addChild(stopButton);

            var resetButton=new LButtonSample1("reset");
            resetButton.x=stopButton.x+100;
            resetButton.y=startButton.y;
            resetButton.addEventListener(LMouseEvent.MOUSE_UP, onClickReset);
            layer.addChild(resetButton);

            // 信息显示
            repeatState=new LTextField();
            repeatState.x=label.x;
            repeatState.y=200;
            layer.addChild(repeatState);
            runningState=new LTextField();
            runningState.x=label.x;
            runningState.y=220;
            layer.addChild(runningState);
        }
        function updateState () {
            repeatState.text="currentCount: "+timer.getCurrentCount();
            runningState.text=timer.isRunning()?"running":"not running";
        }
        function onTimer (event) {
            // var t=event.currentTarget;
            // trace(t.getCurrentCount());
            updateState();
        }
        function onTimerComplete (event) {
            updateState();
        }
        function onClickSetting (event) {
            timer.setDelay(delayText.text);
            timer.setRepeatCount(repeatCountText.text);
        }
        function onClickStart (event) {
            timer.start();
        }
        function onClickStop (event) {
            timer.stop();
            updateState();
        }
        function onClickReset (event) {
            timer.reset();
            updateState();
        }
    </script>
</body>

</html>
